package com.atguigu.gulimall.product.service.impl;

import com.atguigu.gulimall.product.entity.SpuInfoEntity;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.product.dao.SkuInfoDao;
import com.atguigu.gulimall.product.entity.SkuInfoEntity;
import com.atguigu.gulimall.product.service.SkuInfoService;
import org.springframework.util.StringUtils;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }

    // 5.1)、保存sku的基本信息   pms_sku_info
    @Override
    public void saveSkuInfo(SkuInfoEntity skuInfoEntity) {
        save(skuInfoEntity);
    }

    /**
     * 复杂的条件查询
     * @param params
     * @return
     */
    @Override
    public PageUtils queryPageByCondition(Map<String, Object> params) {
        LambdaQueryWrapper<SkuInfoEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        String key = (String) params.get("key");
        if(StringUtils.isEmpty(key)) {
            lambdaQueryWrapper.and((obj) ->{
                obj.eq(SkuInfoEntity::getSkuId,key).or().like(SkuInfoEntity::getSkuName,key);
            });
        }

        // 其他条件的检索
        /*
            catelogId: 0
            brandId: 0
            min: 0
            max: 0
         */
        String catelogId = (String) params.get("catelogId");
        // 还要不等于0才行 不能与就是没有条件
        lambdaQueryWrapper.eq(!StringUtils.isEmpty(catelogId) && !"0".equalsIgnoreCase(catelogId),SkuInfoEntity::getCatelogId,catelogId);

        String brandId = (String) params.get("brandId");
        lambdaQueryWrapper.eq(!StringUtils.isEmpty(brandId) && !"0".equalsIgnoreCase(brandId),SkuInfoEntity::getBrandId,brandId);

        String min = (String) params.get("min");
        // 大于等于
        lambdaQueryWrapper.ge(!StringUtils.isEmpty(min) && "0".equalsIgnoreCase(min),SkuInfoEntity::getPrice,min);

        // 注意我们的max就是必须要我们的小于等于 并且不能等于0因为前端默认就是给我们的是0
        String max = (String) params.get("max");
        // 注意我们的price是big字段不能用equal

        if(!StringUtils.isEmpty(key)) {
            try {
                // 防止传过来的不是数字而是其他的
                BigDecimal bigDecimal = new BigDecimal(max);
                if((bigDecimal.compareTo(new BigDecimal("0"))) == 1) {
                    // 小于等于
                    lambdaQueryWrapper.le(SkuInfoEntity::getPrice,max);
                }
            } catch (Exception e) {

            }
        }


        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                lambdaQueryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public List<SkuInfoEntity> getSkusBySpuId(Long spuId) {

        // 根据spuId查询所有的sku的基本信息
        LambdaQueryWrapper<SkuInfoEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SkuInfoEntity::getSpuId,spuId);
        List<SkuInfoEntity> list = list(lambdaQueryWrapper);

        return list;
    }

    @Override
    public PageUtils listForSkuInfo(Map<String, Object> params, Long catlogId) {

        // 根据key查询
        String key = (String) params.get("key");
        LambdaQueryWrapper<SkuInfoEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();


        if (catlogId != 0) {
            lambdaQueryWrapper.eq(SkuInfoEntity::getCatelogId, catlogId);
        }

        // 获取所有的三级分类的sku_id

        if(!StringUtils.isEmpty(key)) {
            lambdaQueryWrapper.and((obj) -> {
                obj.eq(SkuInfoEntity::getSkuId,key).or().like(SkuInfoEntity::getSkuName,key);
            });
        }

        IPage<SkuInfoEntity> page = page(new Query<SkuInfoEntity>().getPage(params), lambdaQueryWrapper);

        return new PageUtils(page);
    }


}